<template><div><h2 id="laravel-socialite" tabindex="-1"><a class="header-anchor" href="#laravel-socialite"><span>Laravel Socialite</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#installation">安装</a></li>
<li><a href="#upgrading-socialite">升级</a></li>
<li><a href="#configuration">配置</a></li>
<li><a href="#authentication">认证</a>
<ul>
<li><a href="#routing">路由</a></li>
<li><a href="#authentication-and-storage">身份验证和存储</a></li>
<li><a href="#access-scopes">访问范围</a></li>
<li><a href="#optional-parameters">可选参数</a></li>
</ul>
</li>
<li><a href="#retrieving-user-details">检索用户详细信息</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>除了典型的基于表单的身份验证之外，Laravel 还提供了一种使用 <a href="https://github.com/laravel/socialite" target="_blank" rel="noopener noreferrer">Laravel Socialite</a>对 OAuth providers 进行身份验证的简单方便的方法。 Socialite 目前支持 Facebook，Twitter，LinkedIn，Google，GitHub，GitLab 和 Bitbucket 的身份验证。</p>
<blockquote>
<p>技巧：其他平台的驱动器可以在 <a href="https://socialiteproviders.com/" target="_blank" rel="noopener noreferrer">Socialite Providers</a> 社区驱动网站查找。</p>
</blockquote>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>在开始使用 Socialite 之前，通过 Composer 软件包管理器将软件包添加到项目的依赖项中:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/socialite</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="升级" tabindex="-1"><a class="header-anchor" href="#升级"><span>升级</span></a></h2>
<p>升级到 Socialite 的新主要版本时，请务必仔细查看 <a href="https://github.com/laravel/socialite/blob/master/UPGRADE.md" target="_blank" rel="noopener noreferrer">the upgrade guide</a>.</p>
<h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>在使用 Socialite 之前，你需要为应用程序使用的OAuth提供程序添加凭据。通常，可以通过在要验证的服务的仪表板中创建“开发人员应用程序”来检索这些凭据。</p>
<p>这些凭证应该放在你的 <code v-pre>config/services.php</code> 配置文件中， 并且应该使用 <code v-pre>facebook</code>, <code v-pre>twitter</code> (OAuth 1.0), <code v-pre>twitter-oauth-2</code> (OAuth 2.0), <code v-pre>linkedin</code>, <code v-pre>google</code>, <code v-pre>github</code>, <code v-pre>gitlab</code>, 或者 <code v-pre>bitbucket</code>, 取决于应用程序所需的提供商：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'github'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'client_id'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'GITHUB_CLIENT_ID'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'client_secret'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'GITHUB_CLIENT_SECRET'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'redirect'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/callback-url'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>技巧：如果 <code v-pre>redirect</code> 项的值包含一个相对路径，它将会自动解析为全称 URL。</p>
</blockquote>
<h2 id="认证" tabindex="-1"><a class="header-anchor" href="#认证"><span>认证</span></a></h2>
<h3 id="路由" tabindex="-1"><a class="header-anchor" href="#路由"><span>路由</span></a></h3>
<p>要使用 OAuth 提供程序对用户进行身份验证，你需要两个路由：一个用于将用户重定向到 OAuth provider，另一个用于在身份验证后接收来自 provider 的回调。下面的示例控制器演示了这两个路由的实现：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/redirect'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/callback'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// $user->token</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>redirect</code> 提供的方法 <code v-pre>Socialite</code> facade 负责将用户重定向到 OAuth provider，而该 user 方法将读取传入的请求并在身份验证后从提供程序中检索用户的信息。</p>
<h3 id="身份验证和存储" tabindex="-1"><a class="header-anchor" href="#身份验证和存储"><span>身份验证和存储</span></a></h3>
<p>从 OAuth 提供程序检索到用户后，你可以确定该用户是否存在于应用程序的数据库中并<a href="https://learnku.com/docs/laravel/10.x/authentication#authenticate-a-user-instance" target="_blank" rel="noopener noreferrer">验证用户</a>。如果用户在应用程序的数据库中不存在，通常会在数据库中创建一条新记录来代表该用户：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/callback'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$githubUser</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">updateOrCreate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'github_id'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'github_token'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'github_refresh_token'</span> <span class="token operator">=></span> <span class="token variable">$githubUser</span><span class="token operator">-></span><span class="token property">refreshToken</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">login</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>技巧：有关特定 OAuth 提供商提供哪些用户信息的更多信息，请参阅有关 <a href="#retrieving-user-details">检索用户详细信息</a> 的文档。</p>
</blockquote>
<h3 id="访问作用域" tabindex="-1"><a class="header-anchor" href="#访问作用域"><span>访问作用域</span></a></h3>
<p>在重定向用户之前，你还可以使用 <code v-pre>scopes</code> 方法在请求中添加其他「作用域」。此方法会将所有现有作用域与你提供的作用域合并：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">scopes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'read:user'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public_repo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>setScopes</code> 方法覆盖所有现有范围：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">setScopes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'read:user'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'public_repo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="可选参数" tabindex="-1"><a class="header-anchor" href="#可选参数"><span>可选参数</span></a></h3>
<p>许多 OAuth providers 支持重定向请求中的可选参数。 要在请求中包含任何可选参数，请使用关联数组调用 <code v-pre>with</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'google'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'hd'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example.com'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：使用 <code v-pre>with</code> 方法时, 注意不要传递任何保留的关键字，例如 <code v-pre>state</code> 或 <code v-pre>response_type</code>。</p>
</blockquote>
<h2 id="检索用户详细信息" tabindex="-1"><a class="header-anchor" href="#检索用户详细信息"><span>检索用户详细信息</span></a></h2>
<p>在将用户重定向回你的身份验证回调路由之后，你可以使用 Socialite 的 <code v-pre>user</code> 方法检索用户的详细信息。<code v-pre>user</code> 方法为返回的用户对象提供了各种属性和方法，你可以使用这些属性和方法在你自己的数据库中存储有关该用户的信息。</p>
<p>你可以使用不同的属性和方法这取决于要进行身份验证的 OAuth 提供程序是否支持 OAuth 1.0 或 OAuth 2.0：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/auth/callback'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// OAuth 2.0 providers...</span></span>
<span class="line">    <span class="token variable">$token</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$refreshToken</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">refreshToken</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$expiresIn</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">expiresIn</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// OAuth 1.0 providers...</span></span>
<span class="line">    <span class="token variable">$token</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">token</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$tokenSecret</span> <span class="token operator">=</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">tokenSecret</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// All providers...</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getNickname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getEmail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">getAvatar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="从令牌中检索用户详细信息-oauth2" tabindex="-1"><a class="header-anchor" href="#从令牌中检索用户详细信息-oauth2"><span>从令牌中检索用户详细信息 (OAuth2)</span></a></h4>
<p>如果你已经有了一个用户的有效访问令牌，你可以使用 Socialite 的 <code v-pre>userFromToken</code> 方法检索其详细信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">userFromToken</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="从令牌中检索用户详细信息-oauth2-1" tabindex="-1"><a class="header-anchor" href="#从令牌中检索用户详细信息-oauth2-1"><span>从令牌中检索用户详细信息 (OAuth2)</span></a></h4>
<p>如果你已经有了一对有效的用户令牌/秘钥，你可以使用 Socialite 的 <code v-pre>userFromTokenAndSecret</code> 方法检索他们的详细信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'twitter'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">userFromTokenAndSecret</span><span class="token punctuation">(</span><span class="token variable">$token</span><span class="token punctuation">,</span> <span class="token variable">$secret</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="无认证状态" tabindex="-1"><a class="header-anchor" href="#无认证状态"><span>无认证状态</span></a></h4>
<p><code v-pre>stateless</code> 方法可用于禁用会话状态验证。 这在向 API 添加社交身份验证时非常有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Laravel<span class="token punctuation">\</span>Socialite<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Socialite</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Socialite</span><span class="token operator">::</span><span class="token function">driver</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'google'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">stateless</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：Twitter 驱动程序不支持无状态身份验证，它使用 OAuth 1.0 进行身份验证</p>
</blockquote>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/socialite/14916" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/so...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/socialite/14916" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/so...</a></p>
</blockquote>
</div></template>


